package com.jf3q.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.security.entity.EduAclPermission;
import com.jf3q.security.mapper.EduAclPermissionMapper;
import com.jf3q.security.service.EduAclPermissionService;
import com.jf3q.security.service.EduAclRolePermissionService;
import com.jf3q.security.vo.MenuVo;
import com.jf3q.security.vo.MetaVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class EduAclPermissionServiceImpl extends ServiceImpl<EduAclPermissionMapper, EduAclPermission>
    implements EduAclPermissionService {

    @Resource
    EduAclPermissionMapper permissionMapper;
    @Resource
    EduAclRolePermissionService rolePermissionService;

    @Override
    public List<EduAclPermission> getAll() {
        //这个要写sql映射文件，查菜单的时候把角色带回来
        return permissionMapper.getAll();
    }


    @Override
    public List<MenuVo> selectMenuByUserId(String userId) {

        //先根据用户id把其所有的permission查出来
        List<EduAclPermission> permissions=permissionMapper.selectPermissionByUserId(userId);
        //数据复制到MenuVo
        List<MenuVo> menuVos = new ArrayList<>();
        for (int i = 0; i < permissions.size(); i++) {


            if(StringUtils.hasText(permissions.get(i).getPath()) && StringUtils.hasText(permissions.get(i).getComponent())){
                //没路由的那些permissions就没必要返回给前端了

                MenuVo vo =new MenuVo();
                MetaVo meta = new MetaVo();
                meta.setTitle(permissions.get(i).getName());
                meta.setIcon(permissions.get(i).getIcon());
                if(permissions.get(i).getType() ==1){
//
                    vo.setRedirect("noredirect");
//                vo.setRedirect(permissions.get(i).getChildren().get(0).getPath());

                }
                if(permissions.get(i).getType() ==3){
                    vo.setHidden(true);
                }else{
                    vo.setHidden(false);
                }
                vo.setMeta(meta);
                vo.setName("name_"+i);
                vo.setId(permissions.get(i).getId());
                vo.setPid(permissions.get(i).getPid());
                vo.setComponent(permissions.get(i).getComponent());
                vo.setPath(permissions.get(i).getPath());
                menuVos.add(vo);

            }

        }
        //递归查询重新封装数据
        List<MenuVo> trees = new ArrayList<>();
        for (MenuVo treeNode : menuVos) {
            if ("1".equals(treeNode.getPid())) {

                trees.add(findChildren(treeNode,menuVos));
            }
        }
        return trees;
    }

    private MenuVo findChildren(MenuVo treeNode, List<MenuVo> menuVos) {

        treeNode.setChildren(new ArrayList<MenuVo>());

        for (MenuVo it : menuVos) {
            if(treeNode.getId().equals(it.getPid())) {

                treeNode.getChildren().add(findChildren(it,menuVos));
            }
        }
        return treeNode;
    }

/**
     * 递归查找子节点
     * @param treeNodes
     * @return
     */
    private static EduAclPermission findChildren(EduAclPermission treeNode,List<EduAclPermission> treeNodes) {
        treeNode.setChildren(new ArrayList<EduAclPermission>());

        for (EduAclPermission it : treeNodes) {
            if(treeNode.getId().equals(it.getPid())) {
                int level = treeNode.getLevel() + 1;
                it.setLevel(level);
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }


}




